package com.yiwei.zhh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;

import com.yiwei.zhh.R;
import com.yiwei.zhh.adapter.PayWayAdapter;
import com.yiwei.zhh.constant.Const;
import com.yiwei.zhh.model.PayWayModel;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 2017/6/23.
 */

public class PaySelectTypeActivity extends BaseActivity {

    @BindView(R.id.list)
    RecyclerView list;
    private Unbinder unbinder;
    private ArrayList<PayWayModel.DataBean> payWayList;
    private PayWayAdapter adapter;
    private String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentView(R.layout.activity_pay_select_type);
        unbinder = ButterKnife.bind(this);
        baseInitView(this, "PaySelectTypeActivity", Const.ViewType.ACTIVITY);
        initView();
    }

    private void initView() {
        setBackIntent();
        setTitleText("支付方式");
        title = getIntent().getStringExtra(Const.KEY.PAY_FIRST_TITLE);
        payWayList = getIntent().getParcelableArrayListExtra(Const.KEY.PAY_WAY_LIST);
        for (int i = 0; i < payWayList.size(); i++) {
            if (title.equals(payWayList.get(i).payment_name)) {
                payWayList.get(i).isCheck = true;
                break;
            }
        }
        adapter = new PayWayAdapter(this);
        list.setAdapter(adapter);
        adapter.setTitle(title);
        adapter.setShareClickListener(new PayWayAdapter.OnCheckListener() {
            @Override
            public void onClick(String way, String pay_id, String payment_description, String title) {
                for (int i = 0; i < payWayList.size(); i++) {
                    if (title.equals(payWayList.get(i).payment_name)) {
                        payWayList.get(i).isCheck = true;
                    } else {
                        payWayList.get(i).isCheck = false;
                    }
                    adapter.update(payWayList);
                }
                adapter.notifyDataSetChanged();
                Intent intent = new Intent();
                intent.putExtra(Const.KEY.PAY_WAY, way);
                intent.putExtra(Const.KEY.PAY_ID, pay_id);
                intent.putExtra(Const.KEY.PAY_TITLE, title);
                intent.putExtra(Const.KEY.PAY_CONTENT, payment_description);
                setResult(1, intent);
                finish();
            }
        });
        if (payWayList != null) {
            adapter.update(payWayList);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

//    public void setBack() {
//        for (int i = 0; i < payWayList.size(); i++) {
//            if (payWayList.get(i).payment_name.equals(title)) {
//                Intent intent = new Intent();
//                intent.putExtra(Const.KEY.PAY_WAY, payWayList.get(i).payment_class_name);
//                intent.putExtra(Const.KEY.PAY_ID, payWayList.get(i).payment_id);
//                intent.putExtra(Const.KEY.PAY_CONTENT, payWayList.get(i).payment_description);
//                intent.putExtra(Const.KEY.PAY_CONTENT, payWayList.get(i).payment_name);
//                setResult(1, intent);
//                finish();
//                break;
//            }
//        }
//    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }

}
